#!/bin/bash
#root密码自行修改
password=KqO0*PUSf4CB
#21端口对应的映射
port21=21
#1078端口对应的映射
port1078=1078
#ftp连续的两个端口
port_min=42013
port_max=42014
#域名
domain=62592152ut.imdo.co

echo "${password}" | passwd --stdin root

yum install wget epel-release net-tools vim git ntpdate zip unzip -y
ntpdate ntp1.aliyun.com
hwclock -w
echo '--- fix timezone ----'
mv /etc/localtime /etc/localtimebak
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if ! crontab -l |grep ntpdate &>/dev/null ; then
    (echo "* 1 * * * ntpdate ntp1.aliyun.com >/dev/null 2>&1";hwclock -w;crontab -l) |crontab 
fi
echo '--- redis install ----'
yum install redis -y
systemctl enable redis
echo '--- nginx install ----'
yum install nginx -y
systemctl enable nginx



echo '--- java install ----'
mkdir -p /usr/local/jdk/jdk17
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
tar -zxf jdk-17_linux-x64_bin.tar.gz  -C /usr/local/jdk/jdk17 --strip-components 1
# 配置环境变量
if [ "$JAVA_HOME" == "" ];then
	echo '#java environment' >> /etc/profile
	echo 'export JAVA_HOME=/usr/local/jdk/jdk17' >> /etc/profile
	echo "export CLASSPATH=.:\${JAVA_HOME}/jre/lib/rt.jar:\${JAVA_HOME}/lib/dt.jar:\${JAVA_HOME}/lib/tools.jar" >> /etc/profile
	echo "export PATH=\$PATH:\${JAVA_HOME}/bin" >> /etc/profile
fi	



source /etc/profile
java -version

echo '--- ftp install ----'
yum install vsftpd -y
systemctl enable vsftpd
mkdir -p /data/ftp
useradd -d /data/ftp -s /sbin/nologin useradmin
echo '123456GgtY3jVt2h'| passwd --stdin useradmin
usermod -d /data/ftp useradmin
chown -Rc useradmin. /data/ftp
firewall-cmd --permanent --zone=public --add-service=ftp
firewall-cmd --reload
setsebool -P ftpd_full_access on
echo "useradmin" > /etc/vsftpd/chroot_list

echo '--- replace ftp config ----'

echo "pasv_min_port=${port_min}" >> ./peaunt/vsftpd.conf
echo "pasv_max_port=${port_max}" >> ./peaunt/vsftpd.conf
mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak
cp ./peaunt/vsftpd.conf /etc/vsftpd/vsftpd.conf

mv /etc/pam.d/vsftpd /etc/pam.d/vsftpd_bak
cp ./peaunt/vsftpd /etc/pam.d/vsftpd

echo '--- restart ftp ----'
systemctl restart vsftpd

echo '--- replace nginx config ----'
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_bak
cp ./peaunt/nginx.conf /etc/nginx/nginx.conf
echo '--- restart nginx ----'
systemctl start nginx


echo '--- phddns install ----'
wget https://down.oray.com/hsk/linux/phddns_5.2.0_amd64.rpm
rpm -ivh phddns_5.2.0_amd64.rpm


mkdir -p /data/peaunt/config

echo "ftp.password=123456GgtY3jVt2h" >> ./peaunt/application.properties
echo "peaunt.domain=${domain}" >> ./peaunt/application.properties
echo "store.host=https://${domain}/img" >> ./peaunt/application.properties
echo "live.server.stream.port=${port1078}" >> ./peaunt/application.properties
echo "ftp.port=${port21}" >> ./peaunt/application.properties

cp ./peaunt/application.properties /data/peaunt/config/
touch /data/peaunt/startup.sh
echo "nohup /usr/local/jdk/jdk17/bin/java -jar /data/peaunt/peaunt-boot-1.0-SNAPSHOT.jar --spring.profiles.active=production >/data/peaunt/server.log &" >> /data/peaunt/startup.sh
chmod +x /data/peaunt/startup.sh
cp ./peaunt/peaunt.service /usr/lib/systemd/system/peaunt.service

systemctl enable peaunt.service
systemctl start peaunt.service